﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Messaging;

namespace ProjectSpam
{
    public partial class Backup : Form
    {

        string mDir;
        Boolean compress;
        const string sendQueue = ".\\private$\\projectSpamQueue";
        //const string sendQueue = "FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112";

        

        
        public Backup(string dir)
        {
            InitializeComponent();


            mDir = dir;

            ListDirectory(this.treeView, dir);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            //Do some messaing stuff
            List<String> checkedItems = this.CheckedNames(this.treeView.Nodes, "",0);

            //insert name, compressflag, absoluteopath to list
            //checkedItems.Insert(0, mDir);
            //checkedItems.Insert(0, "" + compress);

            if (textBoxName.Text.Equals(""))
            {
                MessageBox.Show("Please insert a Backup-Name");
                return;
            }
            //checkedItems.Insert(0, textBoxName.Text);
            //send

            //TEST
            Packer packer = new Packer();
            packer.backUpFiles(this.compress,this.textBoxName.Text, mDir, checkedItems);

            SendMessageToQueue( checkedItems);

            //...Error homoklada...//

        }

        private static void SendMessageToQueue(List<string> files)
        {
        // check if queue exists, if not create it
            
            MessageQueue msMq = null;
            try
            {
                if (!MessageQueue.Exists(sendQueue))
                {
                    msMq = MessageQueue.Create(sendQueue);
                }
                else
                {
                    msMq = new MessageQueue(sendQueue);
                }
            }
            catch (MessageQueueException e)
            {

            }

            try
            {
                msMq.Send("Sending data to MSMQ at " + DateTime.Now.ToString());

                System.Messaging.Message newMessage = new System.Messaging.Message(files);
                msMq.Send(newMessage);
               // msMq.Send(files);
            }
            catch (MessageQueueException ee)
            {
                Console.Write(ee.ToString());
            }
            catch (Exception eee)
            {
                Console.Write(eee.ToString());
            }
            finally
            {
                 msMq.Close();
            }
            
           
        }
 




        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBoxCompress_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.compress =  this.checkBoxCompress.GetItemChecked(0);
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = treeView.SelectedNode;
            this.textBoxName.Text = node.Text;
        }


        private void ListDirectory(TreeView treeView, string path)
        {
            treeView.Nodes.Clear();
            var rootDirectoryInfo = new System.IO.DirectoryInfo(path);
            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

        private static TreeNode CreateDirectoryNode(System.IO.DirectoryInfo directoryInfo)
        {
            var directoryNode = new TreeNode(directoryInfo.Name);

            foreach (var directory in directoryInfo.GetDirectories())
            {
                try
                {
                    directoryNode.Nodes.Add(CreateDirectoryNode(directory));
                }
                catch (UnauthorizedAccessException e)
                {
                }
            }
            
            foreach (var file in directoryInfo.GetFiles())
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            
           
            return directoryNode;
        }


  

 
        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
           foreach(TreeNode node in treeNode.Nodes)
           {
              node.Checked = nodeChecked;
              if(node.Nodes.Count > 0)
              {
                 
                 this.CheckAllChildNodes(node, nodeChecked);
              }
           }
        }

        private void treeview_AfterCheck(object sender, TreeViewEventArgs e)
        {
         
           if(e.Action != TreeViewAction.Unknown)
           {
              if(e.Node.Nodes.Count > 0)
              {
                
                 this.CheckAllChildNodes(e.Node, e.Node.Checked);
              }
           }
        }

        private void Backup_Load(object sender, EventArgs e)
        {

        }


        List<String> CheckedNames(System.Windows.Forms.TreeNodeCollection theNodes, string parent, int depth)
        {
            List<String> aResult = new List<String>();

            if (theNodes != null)
            {
                foreach (System.Windows.Forms.TreeNode aNode in theNodes)
                {
                    string path;
                    if (depth >= 1)
                    {

                        if (aNode.Checked)
                        {
                            if (aNode.Nodes.Count == 0)
                            {
                                aResult.Add(parent + aNode.Text);
                            }
                        }

                        path = parent + aNode.Text + "\\";
                    }
                    else
                    {
                        if (aNode.Checked)
                        {

                            if (aNode.Nodes.Count == 0)
                            {
                                aResult.Add(aNode.Text);
                            }
                        }

                        path = "";
                    }



                    aResult.AddRange(CheckedNames(aNode.Nodes, path, ++depth));
                }
            }

            return aResult;
        }
    }
}
